import {createSlice,Dispatch} from '@reduxjs/toolkit'
import {getMenusApi} from '@/api/menus'
import {setStorage,getStorage} from '@/utils/storeutil'
import {MENUKEY} from '@/utils/constant'

const menuSlice=createSlice({
    name:'menuNS',
    initialState:{
        menus:getStorage(MENUKEY)||[]
    },
    reducers:{
        SET_MENUS(state,action){
            state.menus=action.payload
            setStorage(MENUKEY,action.payload)
        }
    }
})
export const getMenuAsync=(payload:string)=>{
    return async(dispath:Dispatch)=>{
        console.log('---------getMenuAsync---------------');
        const result=await getMenusApi(payload)
        console.log('result.data[0].children',result.data[0].children);
        dispath(SET_MENUS(result.data[0].children))
    }
}

export const {SET_MENUS}=menuSlice.actions
export default menuSlice.reducer



